package com.pshcp.api;

import com.alibaba.fastjson.JSONObject;
import com.external.hospital.ad.batch.Hospital2BatchMessageService;
import com.external.hospital.ad.followup.Hospital2FollowupService;
import com.external.hospital.ad.patient.Hospital2PatientService;
import com.external.hospital.ad.patientgroup.Hospital2PatientGroupService;
import com.external.hospital.ad.quickreply.Hospital2QuickReplyService;
import com.external.hospital.ap.Hospital2UserApService;
import com.external.hospital.ap.UserApManager;
import com.external.hospital.push.Hospital2PushService;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.util.SystemUtil;
import com.pshcp.domain.model.Patient;
import com.pshcp.manager.messagenotification.MessageNotificationManager;
import com.pshcp.service.PatientService;
import com.pshcp.tool.HospitalTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2023/2/12 17:35
 */
@Api(tags = {"通用接口-互联网医院"})
@Slf4j
@RestController
@RequestMapping("/api/hospital")
public class HospitalAPI {

    @Autowired
    private UserApManager userApManager;

    @Autowired
    private MessageNotificationManager messageNotificationManager;

    @Autowired
    private Hospital2BatchMessageService hospital2BatchMessageService;

    @Autowired
    private Hospital2FollowupService hospital2FollowupService;

    @Autowired
    private Hospital2PatientService hospital2PatientService;

    @Autowired
    private Hospital2PatientGroupService hospital2PatientGroupService;

    @Autowired
    private Hospital2UserApService hospital2UserApService;

    @Autowired
    private PatientService patientService;

    @Autowired
    private Hospital2QuickReplyService hospital2QuickReplyService;

    @Autowired
    private Hospital2PushService hospital2PushService;

    @ApiOperation("患者管理:获取患者分组列表")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "患者管理:获取患者分组列表")
    @PostMapping("user/ad/patientGroup/list")
    public JSONObject user2ad2patientGroup2list(@RequestBody Map<String, String> params) throws Exception {
        return hospital2PatientGroupService.user2ad2patientGroup2list(params);
    }

    @ApiOperation("患者管理:获取分组中患者列表")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "患者管理:获取分组中患者列表")
    @PostMapping("user/ad/patientGroup/listUser")
    public JSONObject user2ad2patientGroup2listUser(@RequestBody Map<String, String> params) throws Exception {
        return hospital2PatientGroupService.user2ad2patientGroup2listUser(params);
    }

    @ApiOperation("患者管理:添加患者分组")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.ADD, desc = "患者管理:添加患者分组")
    @PostMapping("user/ad/patientGroup/add")
    public JSONObject user2ad2patientGroup2add(@RequestBody Map<String, String> params) throws Exception {
        return hospital2PatientGroupService.user2ad2patientGroup2add(params);
    }

    @ApiOperation("患者管理:删除患者分组")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.LOGIC_DEL, desc = "患者管理:删除患者分组")
    @PostMapping("user/ad/patientGroup/remove")
    public JSONObject user2ad2patientGroup2remove(@RequestBody Map<String, String> params) throws Exception {
        return hospital2PatientGroupService.user2ad2patientGroup2remove(params);
    }

    @ApiOperation("患者管理:修改患者分组名")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "患者管理:修改患者分组名")
    @PostMapping("user/ad/patientGroup/modify")
    public JSONObject user2ad2patientGroup2modify(@RequestBody Map<String, String> params) throws Exception {
        return hospital2PatientGroupService.user2ad2patientGroup2modify(params);
    }

    @ApiOperation("患者管理:向分组中添加患者")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.ADD, desc = "患者管理:向分组中添加患者")
    @PostMapping("user/ad/patientGroup/addUser")
    public JSONObject user2ad2patientGroup2addUser(@RequestBody Map<String, String> params) throws Exception {
        return hospital2PatientGroupService.user2ad2patientGroup2addUser(params);
    }

    @ApiOperation("患者管理:从分组中移除患者")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.LOGIC_DEL, desc = "患者管理:从分组中移除患者")
    @PostMapping("user/ad/patientGroup/removeUser")
    public JSONObject user2ad2patientGroup2removeUser(@RequestBody Map<String, String> params) throws Exception {
        return hospital2PatientGroupService.user2ad2patientGroup2removeUser(params);
    }

    @ApiOperation("患者管理:获取医生所有分组")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.LOGIC_DEL, desc = "患者管理:获取医生所有分组")
    @PostMapping("user/ad/patientGroup/list/all")
    public JSONObject user2ad2patientGroup2List2All(@RequestBody Map<String, String> params) throws Exception {
        return hospital2PatientGroupService.user2ad2patientGroup2List2All(params);
    }

    @ApiOperation("患者管理:从分组添加或者移除患者")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.LOGIC_DEL, desc = "患者管理:从分组添加或者移除患者")
    @PostMapping("user/ad/patientGroup/modify/patient")
    public JSONObject user2ad2patientGroup2ModifyPatient(@RequestBody Map<String, String> params) throws Exception {
        return hospital2PatientGroupService.user2ad2patientGroup2ModifyPatient(params);
    }

    @ApiOperation("患者管理:删除患者")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.LOGIC_DEL, desc = "患者管理:删除患者")
    @PostMapping("user/ad/patient/delete")
    public JSONObject user2ad2patient2delete(@RequestBody Map<String, String> params) throws Exception {
        return hospital2PatientService.user2ad2patient2delete(params);
    }

    @ApiOperation("患者管理:我的患者列表")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "患者管理:我的患者列表")
    @PostMapping("user/ad/patient/my")
    public JSONObject user2ad2patient2my(@RequestBody Map<String, String> params) throws Exception {
        return hospital2PatientService.user2ad2patient2my(params);
    }

    @ApiOperation("患者管理:患者ABC列表")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "患者管理:患者ABC列表")
    @PostMapping("user/ad/patient/abcList")
    public JSONObject user2ad2patient2abcList(@RequestBody Map<String, String> params) throws Exception {
        return hospital2PatientService.user2ad2patient2abcList(params);
    }

    @ApiOperation("患者管理:修改患者备注信息")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "患者管理:修改患者备注信息")
    @PostMapping("user/ad/patient/modify")
    public JSONObject user2ad2patient2modify(@RequestBody Map<String, String> params) throws Exception {
        return hospital2PatientService.user2ad2patient2modify(params);
    }

    @ApiOperation("患者管理:患者详情")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "患者管理:患者详情")
    @PostMapping("user/ad/patient/detail")
    public JSONObject user2ad2patient2detail(@RequestBody Map<String, String> params) throws Exception {
        return hospital2PatientService.user2ad2patient2detail(params);
    }


    @ApiOperation("群发:发送")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "群发:发送")
    @PostMapping("user/ad/batch/message/send")
    public JSONObject user2ad2batch2message2send(@RequestBody Map<String, String> params) throws Exception {
        return hospital2BatchMessageService.user2ad2batch2message2send(params);
    }

    @ApiOperation("群发:群发历史")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "群发:群发历史")
    @PostMapping("user/ad/batch/message/list")
    public JSONObject user2ad2batch2message2list(@RequestBody Map<String, String> params) throws Exception {
        return hospital2BatchMessageService.user2ad2batch2message2list(params);
    }

    @ApiOperation("群发:消息删除")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.LOGIC_DEL, desc = "群发:消息删除")
    @PostMapping("user/ad/batch/message/remove")
    public JSONObject user2ad2batch2message2remove(@RequestBody Map<String, String> params) throws Exception {
        return hospital2BatchMessageService.user2ad2batch2message2remove(params);
    }

    @ApiOperation("群发:获取分组列表")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "群发:获取分组列表")
    @PostMapping("user/ad/batch/message/group/list")
    public JSONObject user2ad2batch2message2group2list(@RequestBody Map<String, String> params) throws Exception {
        return hospital2BatchMessageService.user2ad2batch2message2group2list(params);
    }

    @ApiOperation("随访:新增随访计划")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.ADD, desc = "随访:新增随访计划")
    @PostMapping("/user/ad/followup")
    public JSONObject user2ad2followup(@RequestBody JSONObject jsonObject) throws Exception {
        return hospital2FollowupService.user2ad2followup(jsonObject);
    }

    @ApiOperation("随访:随访计划列表")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.ADD, desc = "随访:新增随访计划")
    @PostMapping("/user/ad/followup/list")
    public JSONObject user2ad2followup2list(@RequestBody Map<String, String> params) throws Exception {
        return hospital2FollowupService.user2ad2followup2list(params);
    }

    @ApiOperation("随访:医生端获取单个随访详情（包含复诊和问卷详情）")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "随访:医生端获取单个随访详情（包含复诊和问卷详情）")
    @PostMapping("user/ad/followup/detail")
    public JSONObject user2ad2followup2detail(@RequestBody Map<String, String> params) throws Exception {
        return hospital2FollowupService.user2ad2followup2detail(params);
    }

    @ApiOperation("随访:获取随访表单类型")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "随访:获取随访表单类型")
    @PostMapping("user/ad/followup/form/type")
    public JSONObject user2ad2followup2form2type(@RequestBody Map<String, String> params) throws Exception {
        return hospital2FollowupService.user2ad2followup2form2type(params);
    }

    @ApiOperation("随访:分页获取随访表单集合列表")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "随访:分页获取随访表单集合列表")
    @PostMapping("user/ad/followup/form")
    public JSONObject user2ad2followup2form(@RequestBody Map<String, String> params) throws Exception {
        return hospital2FollowupService.user2ad2followup2form(params);
    }

    @ApiOperation("随访:获取添加随访选项")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "随访:获取添加随访选项")
    @PostMapping("user/ad/followup/option")
    public JSONObject user2ad2followup2option(@RequestBody Map<String, String> params) throws Exception {
        return hospital2FollowupService.user2ad2followup2option(params);
    }

    @ApiOperation("随访:删除随访计划")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.LOGIC_DEL, desc = "随访:删除随访计划")
    @PostMapping("user/ad/followup/delete")
    public JSONObject user2ad2followup2delete(@RequestBody Map<String, String> params) throws Exception {
        return hospital2FollowupService.user2ad2followup2delete(params);
    }

    @ApiOperation("随访:医生某月的随访计划日期展示")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "随访:医生某月的随访计划日期展示")
    @PostMapping("user/ad/followup/month")
    public JSONObject user2ad2followup2month(@RequestBody Map<String, String> params) throws Exception {
        return hospital2FollowupService.user2ad2followup2month(params);
    }

    @ApiOperation("随访:医生某天的随访计划")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "随访:医生某天的随访计划")
    @PostMapping("user/ad/followup/today")
    public JSONObject user2ad2followup2today(@RequestBody Map<String, String> params) throws Exception {
        return hospital2FollowupService.user2ad2followup2today(params);
    }


    @ApiOperation("随访:医生端获取包含多次复诊的计划详情")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "随访:医生端获取包含多次复诊的计划详情")
    @PostMapping("user/ad/followup/subsequent/visit/detail")
    public JSONObject user2ad2followup2subsequent2visit2detail(@RequestBody Map<String, String> params) throws Exception {
        return hospital2FollowupService.user2ad2followup2subsequent2visit2detail(params);
    }

    @ApiOperation("随访:修改随访为报道，取消")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "随访:修改随访为报道，取消")
    @PostMapping("user/ad/followup/setting")
    public JSONObject user2ad2followup2setting(@RequestBody Map<String, String> params) throws Exception {
        return hospital2FollowupService.user2ad2followup2setting(params);
    }

    @ApiOperation("患者:添加患者医生")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "随访:添加患者医生")
    @PostMapping("user/ap/patientSync/addPersonalDoctor")
    public JSONObject user2ad2patientSync2addPersonalDoctor(@RequestBody Map<String, String> params) throws Exception {
        //sync to platform
        Long patientId = HospitalTool.buildUser2adrpatientId(params);
        Patient entity = patientService.findById(patientId);
        userApManager.savePatientInquirer(entity);
        return hospital2UserApService.user2ap2patientSync2addPersonalDoctor(params);
    }


    @ApiOperation("患者:医生审核患者（接受）")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "患者:医生审核患者（接受）")
    @PostMapping("user/ap/patientSync/auditDoctorPatient")
    public JSONObject user2ad2patientSync2AuditDoctorPatient(@RequestBody Map<String, String> params) throws Exception {
        // 发送系统消息
        userApManager.sendSystemMessage(HospitalTool.buildUser2ad2patientSync2AuditDoctorPatient2DoctorId(params),
                HospitalTool.buildUser2ad2patientSync2AuditDoctorPatient2PatientId(params), UserApManager.TYPE_ADD_PATIENT);
        messageNotificationManager.sendMessageNotification(HospitalTool.buildUser2ad2patientGroup2addUser2MessageNotificationDTO(HospitalTool.buildUser2ad2patientSync2AuditDoctorPatient2DoctorId(params),HospitalTool.buildUser2ad2patientSync2AuditDoctorPatient2PatientId(params)));
        return hospital2UserApService.user2ap2patientSync2AuditDoctorPatient(params);
    }

    @ApiOperation("患者:医生查询待审核患者列表(分页)")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "患者:医生查询待审核患者列表(分页)")
    @PostMapping("user/ap/patientSync/queryDoctorPatientAuditPage")
    public JSONObject user2ad2patientSync2QueryDoctorPatientAuditPage(@RequestBody Map<String, String> params) throws Exception {
        return hospital2UserApService.user2ap2patientSync2QueryDoctorPatientAuditPage(params);
    }

    @ApiOperation("患者:医生解除患者关系")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "患者:医生解除患者关系")
    @PostMapping("user/ap/patientSync/removeDoctorPatient")
    public JSONObject user2ad2patientSync2RemoveDoctorPatient(@RequestBody Map<String, String> params) throws Exception {
        return hospital2UserApService.user2ap2patientSync2RemoveDoctorPatient(params);
    }

    @ApiOperation("患者:查询医生、患者是否是好友")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "患者:查询医生、患者是否是好友")
    @PostMapping("user/ap/patientSync/queryDoctorPatientIsFriend")
    public JSONObject user2ap2patientSync2queryDoctorPatientIsFriend(@RequestBody Map<String, String> params) throws Exception {
        return hospital2UserApService.user2ap2patientSync2queryDoctorPatientIsFriend(params);
    }


    @ApiOperation("图文问诊:获取快捷回复")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.ADD, desc = "图文问诊:获取快捷回复")
    @PostMapping("/user/ad/quickReply/list")
    public JSONObject user2Ad2QuickReply2List(@RequestBody Map<String, String> params) throws Exception {
        return hospital2QuickReplyService.user2Ad2QuickReply2List(params);
    }

    @ApiOperation("图文问诊:新增快捷回复")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.ADD, desc = "图文问诊:新增快捷回复")
    @PostMapping("/user/ad/quickReply/add")
    public JSONObject user2Ad2QuickReply2Add(@RequestBody Map<String, String> params) throws Exception {
        return hospital2QuickReplyService.user2Ad2QuickReply2Add(params);
    }

    @ApiOperation("图文问诊:删除快捷回复")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.ADD, desc = "图文问诊:删除快捷回复")
    @PostMapping("/user/ad/quickReply/remove")
    public JSONObject user2Ad2QuickReply2Remove(@RequestBody Map<String, String> params) throws Exception {
        return hospital2QuickReplyService.user2Ad2QuickReply2Remove(params);
    }

    @ApiOperation("患者:登录设备校验(登录互踢)")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.ADD, desc = "患者:登录设备校验(登录互踢)")
    @PostMapping("/user/ap/device/verify")
    public JSONObject user2ap2device2verify(@RequestBody Map<String, String> params) throws Exception {
        return hospital2UserApService.user2ap2device2verify(params);
    }

    @ApiOperation("医生:登录设备校验(登录互踢)")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.ADD, desc = "医生:登录设备校验(登录互踢)")
    @PostMapping("/user/ad/device/verify")
    public JSONObject user2ad2device2verify(@RequestBody Map<String, String> params, HttpServletRequest request) throws Exception {
        params.put("ip", SystemUtil.getClientIP(request));
        return hospital2PatientService.user2ad2device2verify(params);
    }

    @ApiOperation("push:添加Android token信息")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.ADD, desc = "push:添加Android token信息")
    @PostMapping("push/deviceToken/addAndroid")
    public JSONObject basic2push2deviceToken2addAndroid(@RequestBody Map<String, String> params, HttpServletRequest request) throws Exception {
        params.put("ip", SystemUtil.getClientIP(request));
        // 更新登录缓存  用户类型(医生d,患者p)
        if("d".equals(params.get("userType"))){
            params.put("doctorId", params.get("userId"));
            hospital2PatientService.user2ad2device2updateDeviceSN(params);
        }else if("p".equals(params.get("userType"))){
            params.put("patientId", params.get("userId"));
            hospital2UserApService.user2ad2device2updateDeviceSN(params);
            // 修改用户患者最后登录时间
            patientService.updatePatientLastHandleTime(Long.parseLong(params.get("userId")));
        }
        return hospital2PushService.basic2push2deviceToken2addAndroid(params);
    }
    @ApiOperation("push:添加IOS token信息")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.ADD, desc = "push:添加IOS token信息")
    @PostMapping("push/deviceToken/addIos")
    public JSONObject basic2push2deviceToken2addIos(@RequestBody Map<String, String> params, HttpServletRequest request) throws Exception {
        params.put("ip", SystemUtil.getClientIP(request));
        // 更新登录缓存  用户类型(医生d,患者p)
        if("d".equals(params.get("userType"))){
            params.put("doctorId", params.get("userId"));
            hospital2PatientService.user2ad2device2updateDeviceSN(params);
        }else if("p".equals(params.get("userType"))){
            params.put("patientId", params.get("userId"));
            hospital2UserApService.user2ad2device2updateDeviceSN(params);
            // 修改用户患者最后登录时间
            patientService.updatePatientLastHandleTime(Long.parseLong(params.get("userId")));
        }
        return hospital2PushService.basic2push2deviceToken2addIos(params);
    }
    @ApiOperation("push:删除用户的token")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.ADD, desc = "push:删除用户的token")
    @PostMapping("push/deviceToken/delDevice")
    public JSONObject basic2push2deviceToken2delDevice(@RequestBody Map<String, String> params) throws Exception {
        return hospital2PushService.basic2push2deviceToken2delDevice(params);
    }

}
